package com.yuexunit.operationman.nbsy.module.OperationInventory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.OperationInventory.adapter.AddInventoryAdapter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.InventoryBean;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.NewGoodsBean;
import com.yuexunit.operationman.nbsy.module.OperationInventory.model.impl.AddGoodsModel;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.IAddGoodsPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.impl.AddGoodsPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.ui.view.IAddGoodsView;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/8/14 0014.
 */

public class Act_OperationInventory_AddList extends BaseKJActivity implements IAddGoodsView{

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.search_txt)
    EditText searchTxt;
    @Bind(R.id.listview)
    FixedSwipeListView listview;

    AddInventoryAdapter adapter;
    ArrayList<NewGoodsBean.ObjectEntity> list = new ArrayList<>();
    ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> checkItemListEntities = new ArrayList<>();
    IAddGoodsPresenter presenter;

    LoadingDialog loadingDialog;


    @Override
    public void setRootView() {
        setContentView(R.layout.act_operationinventory_list);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        setTitle();
        loadingDialog = new LoadingDialog(this);
        presenter = new AddGoodsPresenter(this,new AddGoodsModel());
        checkItemListEntities = (ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity>) getIntent().getExtras().get("goods");
        searchTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (adapter != null) {
                    adapter.getFilter().filter(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onResume(){
        super.onResume();
        Map<String, String> params = new HashMap<>();
        presenter.getAddGoodsDate(params);
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientName());
        if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.INVENTORYBEAN.getObject().getCheck().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.sure:
                ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> checkItemListEntities = new ArrayList<>();
                for (int i =0;i<adapter.getGoodsValue().size();i++)
                {
                    if(adapter.getGoodsValue().get(i).getType() == 1)
                    {
                        InventoryBean.ObjectEntity.CheckItemListEntity checkItemListEntity = new InventoryBean.ObjectEntity.CheckItemListEntity();
                        checkItemListEntity.setItemCode(adapter.getGoodsValue().get(i).getItemCode());
                        checkItemListEntity.setItemName(adapter.getGoodsValue().get(i).getItemName());
                        if(adapter.getGoodsValue().get(i).getCount() == null )
                        {
                            checkItemListEntity.setBeforeCheck(0);
                        }else {
                            checkItemListEntity.setBeforeCheck(Integer.valueOf(adapter.getGoodsValue().get(i).getCount()));
                        }
                        checkItemListEntity.setCheckId("");
                        checkItemListEntity.setCheckItemId("");
                        checkItemListEntity.setInAdd("");
                        checkItemListEntity.setBeforeCloseBody(0);
                        checkItemListEntity.setAfterCloseBody(0);
                        checkItemListEntity.setAfterSkin(0);

                        checkItemListEntities.add(checkItemListEntity);
                    }
                }
                Bundle bundle =new Bundle();
                bundle.putSerializable("goods", checkItemListEntities);
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(102, mIntent);
                finish();
                break;
        }
    }

    @Override
    public void getAddGoodsDate(NewGoodsBean newGoodsBean) {
        if(newGoodsBean.getCode() == 200)
        {
            for(int i=0;i<newGoodsBean.getObject().size();i++)
            {
                boolean isContain =false;
                for (int j = 0;j<checkItemListEntities.size();j++)
                {
                    if(checkItemListEntities.get(j).getItemCode().equals(newGoodsBean.getObject().get(i).getItemCode()))
                    {
                        isContain = true;
                    }
                }
                if(!isContain){
                    NewGoodsBean.ObjectEntity objectEntity = new NewGoodsBean.ObjectEntity();
                    objectEntity = newGoodsBean.getObject().get(i);
                    objectEntity.setIshow(true);
                    list.add(objectEntity);
                }
            }
            adapter = new AddInventoryAdapter(listview, list, R.layout.item_operationinventory);
            listview.setAdapter(adapter);
        }
        else {
            ViewInject.toast("获取物品列表失败");
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
